
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.io.babel &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.babel</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.io.babel</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">OpenBabel interface module, which opens up access to the hundreds of file</span>
<span class="sd">formats supported by OpenBabel. Requires openbabel with python bindings to be</span>
<span class="sd">installed. Please consult the</span>
<span class="sd">`openbabel documentation &lt;http://openbabel.org/wiki/Main_Page&gt;`_.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.structure</span> <span class="kn">import</span> <span class="n">Molecule</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.graphs</span> <span class="kn">import</span> <span class="n">MoleculeGraph</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">requires</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">openbabel</span> <span class="kn">import</span> <span class="n">openbabel</span> <span class="k">as</span> <span class="n">ob</span>
    <span class="kn">from</span> <span class="nn">openbabel</span> <span class="kn">import</span> <span class="n">pybel</span> <span class="k">as</span> <span class="n">pb</span>
<span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
    <span class="n">ob</span> <span class="o">=</span> <span class="kc">None</span>


<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong, Qi Wang&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2012, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;0.1&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyuep@gmail.com&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;Apr 28, 2012&quot;</span>


<div class="viewcode-block" id="BabelMolAdaptor"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor">[docs]</a><span class="k">class</span> <span class="nc">BabelMolAdaptor</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Adaptor serves as a bridge between OpenBabel&#39;s Molecule and pymatgen&#39;s</span>
<span class="sd">    Molecule.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@requires</span><span class="p">(</span><span class="n">ob</span><span class="p">,</span>
              <span class="s2">&quot;BabelMolAdaptor requires openbabel to be installed with &quot;</span>
              <span class="s2">&quot;Python bindings. Please get it at http://openbabel.org &quot;</span>
              <span class="s2">&quot;(version &gt;=3.0.0).&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mol</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes with pymatgen Molecule or OpenBabel&quot;s OBMol.</span>

<span class="sd">        Args:</span>
<span class="sd">            mol: pymatgen&#39;s Molecule or OpenBabel OBMol</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">Molecule</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">mol</span><span class="o">.</span><span class="n">is_ordered</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;OpenBabel Molecule only supports ordered &quot;</span>
                                 <span class="s2">&quot;molecules.&quot;</span><span class="p">)</span>

            <span class="c1"># For some reason, manually adding atoms does not seem to create</span>
            <span class="c1"># the correct OBMol representation to do things like force field</span>
            <span class="c1"># optimization. So we go through the indirect route of creating</span>
            <span class="c1"># an XYZ file and reading in that file.</span>
            <span class="n">obmol</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBMol</span><span class="p">()</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">BeginModify</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">mol</span><span class="p">:</span>
                <span class="n">coords</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">site</span><span class="o">.</span><span class="n">coords</span><span class="p">]</span>
                <span class="n">atomno</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">Z</span>
                <span class="n">obatom</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBAtom</span><span class="p">()</span>
                <span class="n">obatom</span><span class="o">.</span><span class="n">thisown</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">obatom</span><span class="o">.</span><span class="n">SetAtomicNum</span><span class="p">(</span><span class="n">atomno</span><span class="p">)</span>
                <span class="n">obatom</span><span class="o">.</span><span class="n">SetVector</span><span class="p">(</span><span class="o">*</span><span class="n">coords</span><span class="p">)</span>
                <span class="n">obmol</span><span class="o">.</span><span class="n">AddAtom</span><span class="p">(</span><span class="n">obatom</span><span class="p">)</span>
                <span class="k">del</span> <span class="n">obatom</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">ConnectTheDots</span><span class="p">()</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">PerceiveBondOrders</span><span class="p">()</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">SetTotalSpinMultiplicity</span><span class="p">(</span><span class="n">mol</span><span class="o">.</span><span class="n">spin_multiplicity</span><span class="p">)</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">SetTotalCharge</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">mol</span><span class="o">.</span><span class="n">charge</span><span class="p">))</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">Center</span><span class="p">()</span>
            <span class="n">obmol</span><span class="o">.</span><span class="n">EndModify</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span> <span class="o">=</span> <span class="n">obmol</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBMol</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span> <span class="o">=</span> <span class="n">mol</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">pymatgen_mol</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns pymatgen Molecule object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sp</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">coords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">atom</span> <span class="ow">in</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBMolAtomIter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">):</span>
            <span class="n">sp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">atom</span><span class="o">.</span><span class="n">GetAtomicNum</span><span class="p">())</span>
            <span class="n">coords</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">atom</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">atom</span><span class="o">.</span><span class="n">GetY</span><span class="p">(),</span> <span class="n">atom</span><span class="o">.</span><span class="n">GetZ</span><span class="p">()])</span>
        <span class="k">return</span> <span class="n">Molecule</span><span class="p">(</span><span class="n">sp</span><span class="p">,</span> <span class="n">coords</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">openbabel_mol</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns OpenBabel&#39;s OBMol.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span>

<div class="viewcode-block" id="BabelMolAdaptor.localopt"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.localopt">[docs]</a>    <span class="k">def</span> <span class="nf">localopt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forcefield</span><span class="o">=</span><span class="s1">&#39;mmff94&#39;</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="mi">500</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A wrapper to pybel&#39;s localopt method to optimize a Molecule.</span>

<span class="sd">        Args:</span>
<span class="sd">            forcefield: Default is mmff94. Options are &#39;gaff&#39;, &#39;ghemical&#39;,</span>
<span class="sd">                &#39;mmff94&#39;, &#39;mmff94s&#39;, and &#39;uff&#39;.</span>
<span class="sd">            steps: Default is 500.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pbmol</span> <span class="o">=</span> <span class="n">pb</span><span class="o">.</span><span class="n">Molecule</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span>
        <span class="n">pbmol</span><span class="o">.</span><span class="n">localopt</span><span class="p">(</span><span class="n">forcefield</span><span class="o">=</span><span class="n">forcefield</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="n">steps</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span> <span class="o">=</span> <span class="n">pbmol</span><span class="o">.</span><span class="n">OBMol</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.make3d"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.make3d">[docs]</a>    <span class="k">def</span> <span class="nf">make3d</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forcefield</span><span class="o">=</span><span class="s2">&quot;mmff94&quot;</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="mi">50</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A wrapper to pybel&#39;s make3D method generate a 3D structure from a</span>
<span class="sd">        2D or 0D structure.</span>
<span class="sd">        The 3D structure is made very quickly using a combination of rules</span>
<span class="sd">        (e.g. sp3 atoms should have four bonds arranged in a tetrahedron) and</span>
<span class="sd">        ring templates (e.g. cyclohexane is shaped like a chair). Once 3D</span>
<span class="sd">        coordinates are generated, hydrogens are added and a quick local</span>
<span class="sd">        optimization is carried out as default.</span>

<span class="sd">        The generated 3D structure can have clashes or have high energy</span>
<span class="sd">        structures due to some strain. Please consider to use the conformer</span>
<span class="sd">        search or geometry optimization to further optimize the structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            forcefield: Default is mmff94. Options are &#39;gaff&#39;, &#39;ghemical&#39;,</span>
<span class="sd">                &#39;mmff94&#39;, &#39;mmff94s&#39;, and &#39;uff&#39;.</span>
<span class="sd">            steps: Default is 50.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pbmol</span> <span class="o">=</span> <span class="n">pb</span><span class="o">.</span><span class="n">Molecule</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span>
        <span class="n">pbmol</span><span class="o">.</span><span class="n">make3D</span><span class="p">(</span><span class="n">forcefield</span><span class="o">=</span><span class="n">forcefield</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="n">steps</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span> <span class="o">=</span> <span class="n">pbmol</span><span class="o">.</span><span class="n">OBMol</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.add_hydrogen"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.add_hydrogen">[docs]</a>    <span class="k">def</span> <span class="nf">add_hydrogen</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add hydrogens (make all hydrogen explicit).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">AddHydrogens</span><span class="p">()</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.remove_bond"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.remove_bond">[docs]</a>    <span class="k">def</span> <span class="nf">remove_bond</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">idx1</span><span class="p">,</span> <span class="n">idx2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove a bond from an openbabel molecule</span>

<span class="sd">        Args:</span>
<span class="sd">            idx1: The atom index of one of the atoms participating the in bond</span>
<span class="sd">            idx2: The atom index of the other atom participating in the bond</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">obbond</span> <span class="ow">in</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBMolBondIter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">):</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">obbond</span><span class="o">.</span><span class="n">GetBeginAtomIdx</span><span class="p">()</span> <span class="o">==</span> <span class="n">idx1</span> <span class="ow">and</span> <span class="n">obbond</span><span class="o">.</span><span class="n">GetEndAtomIdx</span><span class="p">()</span> <span class="o">==</span> <span class="n">idx2</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span>
                    <span class="n">obbond</span><span class="o">.</span><span class="n">GetBeginAtomIdx</span><span class="p">()</span> <span class="o">==</span> <span class="n">idx2</span> <span class="ow">and</span> <span class="n">obbond</span><span class="o">.</span><span class="n">GetEndAtomIdx</span><span class="p">()</span> <span class="o">==</span> <span class="n">idx1</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">DeleteBond</span><span class="p">(</span><span class="n">obbond</span><span class="p">)</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.rotor_conformer"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.rotor_conformer">[docs]</a>    <span class="k">def</span> <span class="nf">rotor_conformer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">rotor_args</span><span class="p">,</span> <span class="n">algo</span><span class="o">=</span><span class="s2">&quot;WeightedRotorSearch&quot;</span><span class="p">,</span>
                        <span class="n">forcefield</span><span class="o">=</span><span class="s2">&quot;mmff94&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Conformer search based on several Rotor Search algorithms of openbabel.</span>
<span class="sd">        If the input molecule is not 3D, make3d will be called (generate 3D</span>
<span class="sd">        structure, add hydrogen, a quick localopt). All hydrogen atoms need</span>
<span class="sd">        to be made explicit.</span>

<span class="sd">        Args:</span>
<span class="sd">            rotor_args: pass args to Rotor Search in openbabel.</span>
<span class="sd">                for &quot;WeightedRotorSearch&quot;: (conformers, geomSteps,</span>
<span class="sd">                sampleRingBonds-default False)</span>
<span class="sd">                for &quot;SystematicRotorSearch&quot;: (geomSteps-default 2500,</span>
<span class="sd">                sampleRingBonds-default False)</span>
<span class="sd">                for &quot;RandomRotorSearch&quot;: (conformers, geomSteps-default 2500,</span>
<span class="sd">                sampleRingBonds-default False)</span>
<span class="sd">            algo (str): Default is &quot;WeightedRotorSearch&quot;. Options are</span>
<span class="sd">                &quot;SystematicRotorSearch&quot;, &quot;RandomRotorSearch&quot;, and</span>
<span class="sd">                &quot;WeightedRotorSearch&quot;.</span>
<span class="sd">            forcefield (str): Default is mmff94. Options are &#39;gaff&#39;, &#39;ghemical&#39;,</span>
<span class="sd">                &#39;mmff94&#39;, &#39;mmff94s&#39;, and &#39;uff&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">GetDimension</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">make3d</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_hydrogen</span><span class="p">()</span>

        <span class="n">ff</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBForceField_FindType</span><span class="p">(</span><span class="n">forcefield</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ff</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;This input forcefield </span><span class="si">{}</span><span class="s2"> is not supported &quot;</span>
                          <span class="s2">&quot;in openbabel. The forcefield will be reset as &quot;</span>
                          <span class="s2">&quot;default &#39;mmff94&#39; for now.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">forcefield</span><span class="p">))</span>
            <span class="n">ff</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBForceField_FindType</span><span class="p">(</span><span class="s2">&quot;mmff94&quot;</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">rotor_search</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">ff</span><span class="p">,</span> <span class="n">algo</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;This input conformer search algorithm </span><span class="si">{}</span><span class="s2"> is not &quot;</span>
                          <span class="s2">&quot;supported in openbabel. Options are &quot;</span>
                          <span class="s2">&quot;&#39;SystematicRotorSearch&#39;, &#39;RandomRotorSearch&#39; &quot;</span>
                          <span class="s2">&quot;and &#39;WeightedRotorSearch&#39;. &quot;</span>
                          <span class="s2">&quot;The algorithm will be reset as default &quot;</span>
                          <span class="s2">&quot;&#39;WeightedRotorSearch&#39; for now.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">algo</span><span class="p">))</span>
            <span class="n">rotor_search</span> <span class="o">=</span> <span class="n">ff</span><span class="o">.</span><span class="n">WeightedRotorSearch</span>
        <span class="n">rotor_search</span><span class="p">(</span><span class="o">*</span><span class="n">rotor_args</span><span class="p">)</span>
        <span class="n">ff</span><span class="o">.</span><span class="n">GetConformers</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.gen3d_conformer"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.gen3d_conformer">[docs]</a>    <span class="k">def</span> <span class="nf">gen3d_conformer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A combined method to first generate 3D structures from 0D or 2D</span>
<span class="sd">        structures and then find the minimum energy conformer:</span>

<span class="sd">        1. Use OBBuilder to create a 3D structure using rules and ring templates</span>
<span class="sd">        2. Do 250 steps of a steepest descent geometry optimization with the</span>
<span class="sd">           MMFF94 forcefield</span>
<span class="sd">        3. Do 200 iterations of a Weighted Rotor conformational search</span>
<span class="sd">           (optimizing each conformer with 25 steps of a steepest descent)</span>
<span class="sd">        4. Do 250 steps of a conjugate gradient geometry optimization.</span>

<span class="sd">        Warning from openbabel docs:</span>
<span class="sd">        For many applications where 100s if not 1000s of molecules need to be</span>
<span class="sd">        processed, gen3d is rather SLOW. Sometimes this function can cause a</span>
<span class="sd">        segmentation fault.</span>
<span class="sd">        A future version of Open Babel will provide options for slow/medium/fast</span>
<span class="sd">        3D structure generation which will involve different compromises</span>
<span class="sd">        between speed and finding the global energy minimum.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">gen3d</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBOp</span><span class="o">.</span><span class="n">FindType</span><span class="p">(</span><span class="s2">&quot;Gen3D&quot;</span><span class="p">)</span>
        <span class="n">gen3d</span><span class="o">.</span><span class="n">Do</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.confab_conformers"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.confab_conformers">[docs]</a>    <span class="k">def</span> <span class="nf">confab_conformers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forcefield</span><span class="o">=</span><span class="s2">&quot;mmff94&quot;</span><span class="p">,</span> <span class="n">freeze_atoms</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">rmsd_cutoff</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">energy_cutoff</span><span class="o">=</span><span class="mf">50.0</span><span class="p">,</span>
                          <span class="n">conf_cutoff</span><span class="o">=</span><span class="mi">100000</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Conformer generation based on Confab to generate all diverse low-energy</span>
<span class="sd">        conformers for molecules. This is different from rotor_conformer or</span>
<span class="sd">        gen3d_conformer as it aims to not simply to find a low energy</span>
<span class="sd">        conformation but to generate several different conformations.</span>

<span class="sd">        Args:</span>
<span class="sd">            forcefield (str): Default is mmff94. Options are &#39;gaff&#39;, &#39;ghemical&#39;,</span>
<span class="sd">                &#39;mmff94&#39;, &#39;mmff94s&#39;, and &#39;uff&#39;.</span>
<span class="sd">            freeze_atoms ([int]): index of atoms to be freezed when performing</span>
<span class="sd">                conformer search, default is None.</span>
<span class="sd">            rmsd_cutoff (float): rmsd_cufoff, default is 0.5 Angstrom.</span>
<span class="sd">            energy_cutoff (float): energy_cutoff, default is 50.0 kcal/mol.</span>
<span class="sd">            conf_cutoff (float): max number of conformers to test,</span>
<span class="sd">                default is 1 million.</span>
<span class="sd">            verbose (bool): whether to display information on torsions found,</span>
<span class="sd">                default is False.</span>

<span class="sd">        Returns:</span>
<span class="sd">             (list): list of pymatgen Molecule objects for generated conformers.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">GetDimension</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">make3d</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_hydrogen</span><span class="p">()</span>

        <span class="n">ff</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBForceField_FindType</span><span class="p">(</span><span class="n">forcefield</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ff</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Could not find forcefield </span><span class="si">{}</span><span class="s2"> in openbabel, the forcefield &quot;</span>
                  <span class="s2">&quot;will be reset as default &#39;mmff94&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">forcefield</span><span class="p">))</span>
            <span class="n">ff</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBForceField_FindType</span><span class="p">(</span><span class="s2">&quot;mmff94&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">freeze_atoms</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> atoms will be freezed&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">freeze_atoms</span><span class="p">)))</span>
            <span class="n">constraints</span> <span class="o">=</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBFFConstraints</span><span class="p">()</span>

            <span class="k">for</span> <span class="n">atom</span> <span class="ow">in</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBMolAtomIter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">):</span>
                <span class="n">atom_id</span> <span class="o">=</span> <span class="n">atom</span><span class="o">.</span><span class="n">GetIndex</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="nb">id</span> <span class="ow">in</span> <span class="n">freeze_atoms</span><span class="p">:</span>
                    <span class="n">constraints</span><span class="o">.</span><span class="n">AddAtomConstraint</span><span class="p">(</span><span class="n">atom_id</span><span class="p">)</span>
            <span class="n">ff</span><span class="o">.</span><span class="n">SetConstraints</span><span class="p">(</span><span class="n">constraints</span><span class="p">)</span>

        <span class="c1"># Confab conformer generation</span>
        <span class="n">ff</span><span class="o">.</span><span class="n">DiverseConfGen</span><span class="p">(</span><span class="n">rmsd_cutoff</span><span class="p">,</span> <span class="n">conf_cutoff</span><span class="p">,</span> <span class="n">energy_cutoff</span><span class="p">,</span>
                          <span class="n">verbose</span><span class="p">)</span>
        <span class="n">ff</span><span class="o">.</span><span class="n">GetConformers</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span>

        <span class="c1"># Number of conformers generated by Confab conformer generation</span>
        <span class="n">conformer_num</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">NumConformers</span><span class="p">()</span>

        <span class="n">conformers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">conformer_num</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">SetConformer</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="n">conformer</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">BabelMolAdaptor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span><span class="o">.</span><span class="n">pymatgen_mol</span><span class="p">)</span>
            <span class="n">conformers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">conformer</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="o">.</span><span class="n">SetConformer</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">conformers</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">pybel_mol</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns Pybel&#39;s Molecule object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">pb</span><span class="o">.</span><span class="n">Molecule</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span>

<div class="viewcode-block" id="BabelMolAdaptor.write_file"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.write_file">[docs]</a>    <span class="k">def</span> <span class="nf">write_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">file_format</span><span class="o">=</span><span class="s2">&quot;xyz&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Uses OpenBabel to output all supported formats.</span>

<span class="sd">        Args:</span>
<span class="sd">            filename: Filename of file to output</span>
<span class="sd">            file_format: String specifying any OpenBabel supported formats.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mol</span> <span class="o">=</span> <span class="n">pb</span><span class="o">.</span><span class="n">Molecule</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_obmol</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">mol</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">file_format</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.from_file"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.from_file">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">from_file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">file_format</span><span class="o">=</span><span class="s2">&quot;xyz&quot;</span><span class="p">,</span> <span class="n">return_all_molecules</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Uses OpenBabel to read a molecule from a file in all supported formats.</span>

<span class="sd">        Args:</span>
<span class="sd">            filename: Filename of input file</span>
<span class="sd">            file_format: String specifying any OpenBabel supported formats.</span>
<span class="sd">            return_all_molecules: If ``True``, will return a list of</span>
<span class="sd">                ``BabelMolAdaptor`` instances, one for each molecule found in</span>
<span class="sd">                the file. If ``False``, will return only the first molecule.</span>

<span class="sd">        Returns:</span>
<span class="sd">            BabelMolAdaptor object or list thereof</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mols</span> <span class="o">=</span> <span class="n">pb</span><span class="o">.</span><span class="n">readfile</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">file_format</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">return_all_molecules</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">BabelMolAdaptor</span><span class="p">(</span><span class="n">mol</span><span class="o">.</span><span class="n">OBMol</span><span class="p">)</span> <span class="k">for</span> <span class="n">mol</span> <span class="ow">in</span> <span class="n">mols</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">BabelMolAdaptor</span><span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">mols</span><span class="p">)</span><span class="o">.</span><span class="n">OBMol</span><span class="p">)</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.from_molecule_graph"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.from_molecule_graph">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">from_molecule_graph</span><span class="p">(</span><span class="n">mol</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Read a molecule from a pymatgen MoleculeGraph object.</span>

<span class="sd">        Args:</span>
<span class="sd">            mol: pymatgen MoleculeGraph object.</span>

<span class="sd">        Returns:</span>
<span class="sd">            BabelMolAdaptor object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">MoleculeGraph</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">BabelMolAdaptor</span><span class="p">(</span><span class="n">mol</span><span class="o">.</span><span class="n">molecule</span><span class="p">)</span></div>

<div class="viewcode-block" id="BabelMolAdaptor.from_string"><a class="viewcode-back" href="../../../pymatgen.io.babel.html#pymatgen.io.babel.BabelMolAdaptor.from_string">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">from_string</span><span class="p">(</span><span class="n">string_data</span><span class="p">,</span> <span class="n">file_format</span><span class="o">=</span><span class="s2">&quot;xyz&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Uses OpenBabel to read a molecule from a string in all supported</span>
<span class="sd">        formats.</span>

<span class="sd">        Args:</span>
<span class="sd">            string_data: String containing molecule data.</span>
<span class="sd">            file_format: String specifying any OpenBabel supported formats.</span>

<span class="sd">        Returns:</span>
<span class="sd">            BabelMolAdaptor object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mols</span> <span class="o">=</span> <span class="n">pb</span><span class="o">.</span><span class="n">readstring</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">file_format</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">string_data</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">BabelMolAdaptor</span><span class="p">(</span><span class="n">mols</span><span class="o">.</span><span class="n">OBMol</span><span class="p">)</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.babel</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>